//
//  BreadLiveVC.m
//  Bread-Live
//
//  Created by wy on 2017/3/11.
//  Copyright © 2017年 Chris. All rights reserved.
//

#import "BreadLiveVC.h"
#import "HomeTabHeadView.h"
#import "NoMoreMsgView.h"
#import "HomeListModel.h"
#import "HomeTableViewCell.h"

static NSString *homeCell = @"cell";

@interface BreadLiveVC ()<UITableViewDelegate,UITableViewDataSource,HomeTabHeaderViewDelegate>
@property(nonatomic,strong) UITableView      *wTableView;
@property(nonatomic,strong) HomeTabHeadView  *headerView;
@property(nonatomic,strong) NoMoreMsgView    *footerView;
@property(nonatomic,strong) NSMutableArray   *data;

@end

@implementation BreadLiveVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
     [self.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d",2]];
    
    [self addData];
    
     [self.wTableView reloadData];
}

-(void)addData{
    
    self.data = [NSMutableArray array];
    NSDictionary *dic =@{
                         @"name":@"snow xiaochun wangsssss",
                         @"headImgUrl":@"http://images.mianbaolive.com/manage/dd31120b-b680-46d5-b675-82c31e7b15bc.jpg",
                         @"time":@"8天前·直播·END",
                         @"title":@"揭秘宝宝辅食之谜",
                         @"bgUrl":@"http://images.mianbaolive.com/manage/dd31120b-b680-46d5-b675-82c31e7b15bc.jpg",
                         @"fansNumber":@"1000"
                         
                         };
    HomeListModel *mod  = [[HomeListModel alloc] init];
    
    [mod setValuesForKeysWithDictionary:dic];
    
    NSDictionary *dic1 =@{
                         @"name":@"陈大可",
                         @"headImgUrl":@"http://images.mianbaolive.com/manage/796dbec9-6031-42b3-bb46-21fdbc63d7e4.jpg",
                         @"time":@"8天前·直播·END",
                         @"title":@"打造优质简历，帮你敲开企业大门",
                         @"bgUrl":@"http://images.mianbaolive.com/manage/796dbec9-6031-42b3-bb46-21fdbc63d7e4.jpg",
                         @"fansNumber":@"100"
                         
                         };
    HomeListModel *mod1  = [[HomeListModel alloc] init];
    
    [mod1 setValuesForKeysWithDictionary:dic1];
    
    NSDictionary *dic2 =@{
                         @"name":@"大飞",
                         @"headImgUrl":@"http://images.mianbaolive.com/manage/aec9186e-df0f-4bfc-9d63-11fa5e7da2dc.jpg",
                         @"time":@"8天前·直播·END",
                         @"title":@"经常刷新闻和知乎，你为什么没有进步？",
                         @"bgUrl":@"http://images.mianbaolive.com/manage/aec9186e-df0f-4bfc-9d63-11fa5e7da2dc.jpg",
                         @"fansNumber":@"500"
                         
                         };
    HomeListModel *mod2  = [[HomeListModel alloc] init];
    
    [mod2 setValuesForKeysWithDictionary:dic2];
    
    NSDictionary *dic3 =@{
                         @"name":@"星座扒卦馆",
                         @"headImgUrl":@"http://images.mianbaolive.com/manage/7089098e-cd11-4e61-bbf1-211d994b1e6b.jpg",
                         @"time":@"8天前·直播·END",
                         @"title":@"十二星座如何搭配才会幸福",
                         @"bgUrl":@"http://images.mianbaolive.com/manage/7089098e-cd11-4e61-bbf1-211d994b1e6b.jpg",
                         @"fansNumber":@"12"
                         
                         };
    HomeListModel *mod3  = [[HomeListModel alloc] init];
    
    [mod3 setValuesForKeysWithDictionary:dic3];
    
    [self.data addObject:mod];
    [self.data addObject:mod3];
    [self.data addObject:mod1];
    [self.data addObject:mod2];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 5.0;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeListModel *model = self.data[indexPath.row];
    
    CGFloat titleH = [ToolsCustom getSpaceLabelHeight:model.title withFont:[UIFont wyBlodFont:16] withWidth:kWidth-10-45-10 lineSpacing:2];
    
    if (titleH > 21) {
        NSLog(@"2");
        return 21+kWidth/2+70;
    }
    
    NSLog(@"1");
    return kWidth/2+70;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:homeCell];
    
    cell.model = self.data[indexPath.row];
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)refreshListViewTitle:(NSString *)title btnIndex:(NSInteger)index{
    
    NSLog(@"点击按钮刷新控制器%@---%ld",title,index);
}


-(UITableView *)wTableView{
    if (!_wTableView) {
        _wTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0,kWidth , kHeight-64) style:UITableViewStyleGrouped];
        _wTableView.delegate        = self;
        _wTableView.dataSource      = self;
        _wTableView.tableHeaderView = self.headerView;
        _wTableView.tableFooterView = self.footerView;
        
        [_wTableView registerClass:[HomeTableViewCell class] forCellReuseIdentifier:homeCell];
        [self.view addSubview:_wTableView];
    }
    return _wTableView;
}

-(HomeTabHeadView *)headerView{
    if (!_headerView) {
        _headerView = [[HomeTabHeadView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kRate(224))];
        _headerView.delegate = self;
        [_headerView addImageData:@[@"http://bread-live.oss-cn-qingdao.aliyuncs.com/manage/68d31777-82ed-4547-b77e-9f17341e609f.jpg",
                                    @"http://bread-live.oss-cn-qingdao.aliyuncs.com/manage/817f9fcd-8c58-4c5f-ba99-885d325278b5.jpg",
                                    @"http://bread-live.oss-cn-qingdao.aliyuncs.com/manage/0b85acd4-7927-45f7-b7aa-174d5613f8cb.jpg",
                                    @"http://bread-live.oss-cn-qingdao.aliyuncs.com/manage/ca63b23d-2249-46c6-86a7-e61c3f024f56.png",
                                    ]];
        
    }
    return _headerView;
}

-(NoMoreMsgView *)footerView{
    if (!_footerView) {
        _footerView = [[NoMoreMsgView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 80)];
        
    }
    
    return _footerView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
